package cn.xuguowen.user.designPatten.factory.simple.tradition;

import cn.xuguowen.user.designPatten.factory.pizza.CheesePizza;
import cn.xuguowen.user.designPatten.factory.pizza.DurianPizza;
import cn.xuguowen.user.designPatten.factory.pizza.GreekPizza;
import cn.xuguowen.user.designPatten.factory.pizza.Pizza;

import java.io.BufferedReader;
import java.io.InputStreamReader;

/**
 * ClassName: PizzaStore
 * Package: cn.xuguowen.user.designPatten.factory.tradition
 * Description:
 * 可以订购任意的披萨
 * 传统方式下：当我们增加了一种披萨的类型后，订购披萨这个类中的代码逻辑是需要改变的。这就违背了开闭原则。
 * 需要使用简单工厂对其优化：将创建对象的实现封装到简单工厂类中，而不是订购披萨的类中。
 *
 * @Author 徐国文
 * @Create 2023/4/25 14:24
 * @Version 1.0
 */
public class OrderPizza {

    public OrderPizza() {
        Pizza pizza = null;
        String orderType = null;

        do {
            orderType = getPizzaType();
            if (orderType.equals("greek")) {
                pizza = new GreekPizza();
                pizza.setName(" 希腊披萨 ");
            } else if (orderType.equals("cheese")) {
                pizza = new CheesePizza();
                pizza.setName(" 奶酪披萨 ");
            } else if (orderType.equals("durian")) {        // 榴莲披萨就是新增的，为了演示违背开闭原则，使用简单工厂模式优化
                pizza = new DurianPizza();
                pizza.setName(" 榴莲披萨 ");
            } else {
                System.out.println("无法制作该" + orderType + "的披萨");
                break;
            }

            pizza.prepare();
            pizza.bake();
            pizza.cut();
            pizza.box();

        } while (true);
    }

    // 获取用户录入的披萨类型
    public String getPizzaType() {
        String pizzaType = "";
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            System.out.print("input pizza 类型：");
            pizzaType = br.readLine();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return pizzaType;
    }
}
